

import pytest
from API_cases.LLM.APIs import healthy, login, refresh_token, tb_users_list, tb_user_create, tb_user_delete
from utils.logger import log
from utils.random_data_maker import random_data_maker as rd
from config.env import LLM,Unclear







#测试接口——健康
def test_health(method):
    response = healthy(method)
    assert response.json()["status"] == "Healthy"


#测试接口——登录
@pytest.mark.parametrize("username,password,number",[
    ("weichuangxin","xizhi123","001"),
    (rd(length=10,chinese = True),rd(length = 10,letter = True),"002"),
    (rd(length=10, chinese=True), rd(length=10, letter=True), "003")
],ids = ["测试登录——成功","测试登录——失败","测试登录——失败"])
def test_login(method,username,password,number):

    json = {
        "username": username,
        "password": password,
    }

    response = login(method = method,json = json)

    if number == "001":
        assert response.status_code == 200
    if number == "002" or number == "003":
        assert response.status_code == 400


#测试接口——刷新token
@pytest.mark.usefixtures('get_token')
def test_refresh_token(method):

    response = refresh_token(method = method)

    assert response.status_code == 200


#测试接口——查询用户列表
@pytest.mark.usefixtures('get_token')
@pytest.mark.parametrize("number",[
    "001",
    "003",
    "003"
],ids = ["测试有token正确","测试token错误","测试无token"])
def test_tb_users(method,number):

    headers = None
    if number == "001":
        headers = {"authorization": "Bearer " + LLM.access_token}
    if number == "002":
        headers = {"authorization": "Bearer " + rd(length=40, letter=True)}
    if number == "003":
        headers = {"authorization": ""}


    response = tb_users_list(method = method, headers = headers)


    if number == "001":
        assert response.status_code == 200
    if number == "002" or number == "003":
        assert response.status_code == 401 and response.json().get("message") == "Unauthorized"


#测试接口——创建用户
@pytest.mark.usefixtures('get_token')
def test_tb_users_create(method):
    headers = {"authorization": "Bearer " + LLM.access_token}
    json = {
        "username": "test_" + rd(length=4,integer=True,letter=True,punctuation=True,chinese=True),
        "password": rd(length=8,integer=True,letter=True,punctuation=True),
        "role_id": 2,
        "company_id": 15
    }

    response = tb_user_create(method = method, headers = headers, json = json)

    assert response.status_code == 200 and response.json().get("id")

    Unclear.user_id.append(response.json().get("id"))
    log.critical(f"你在此处创建了一个角色，角色ID：{response.json().get("id") }，角色姓名：{json.get('username')}")


#测试接口——删除用户
@pytest.mark.usefixtures('get_token')
def test_tb_user_delete(method):
    headers = {"authorization": "Bearer " + LLM.access_token}
    params = {
        "user_id": "136"
    }


    response = tb_user_delete(method = method, headers = headers, params = params)
    assert response.status_code == 200















